package com.sunspring.zdcall.quartz;

import com.sunspring.zdcall.common.exception.ZdException;
import com.sunspring.zdcall.quartz.entity.RegisterJobEntity;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;

/**
 * scheduler工具类
 * @author wulei
 * @date 2018-12-26
 **/
@Component
@Slf4j
public class SchedulerManager {

    @Autowired
    private Scheduler scheduler;

    /**
     * 注册一个scheduler
     * @param rje RegisterJobEntity实体类
     * @param clazz job接口的实现类
     */
    public void register(RegisterJobEntity rje,Class<? extends Job> clazz) throws SchedulerException {
        JobKey jobKey = JobKey.jobKey(rje.getJobName(),rje.getJobGroup());
        if(scheduler.getJobDetail(jobKey) != null) {
            throw new ZdException("注册定时任务失败，定时任务" + jobKey + " 已存在");
        }
        JobDetail jobDetail = newJob(clazz)
                .withIdentity(rje.getJobName(),rje.getJobGroup())
                .usingJobData("source", rje.getSource())
                .usingJobData("userType",rje.getType())
                .withDescription(rje.getDescription())
                .build();

        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(rje.getCronExpression());
        Trigger trigger = newTrigger().withIdentity(rje.getTriggerName(),rje.getTriggerGroup())
                .withSchedule(cronScheduleBuilder)
                .build();
        scheduler.scheduleJob(jobDetail,trigger);
        log.info("注册定时任务成功，jobKey：" + JobKey.jobKey(rje.getJobName(),rje.getJobGroup()));
    }

    /**
     * 移除定时任务
     * @param jobName  job名称
     * @param jobGroup job所在分组名称
     */
    public void remove(String jobName,String jobGroup) throws SchedulerException {
        JobKey jobKey = jobCheck(jobName,jobGroup);
        scheduler.deleteJob(jobKey);
        log.info("移除定时任务成功，jobKey：" + jobKey);
    }

    /**
     * 暂停定时任务
     * @param jobName  job名称
     * @param jobGroup job所在分组名称
     */
    public void pause(String jobName,String jobGroup) throws SchedulerException {
        JobKey jobKey = jobCheck(jobName,jobGroup);
        scheduler.pauseJob(jobKey);
        log.info("暂停定时任务成功，jobKey：" + jobKey);
    }

    /**
     * 恢复定时任务
     * @param jobName  job名称
     * @param jobGroup job所在分组名称
     */
    public void resume(String jobName,String jobGroup) throws SchedulerException {
        JobKey jobKey = jobCheck(jobName,jobGroup);
        scheduler.resumeJob(jobKey);
        log.info("恢复定时任务成功，jobKey：" + jobKey);
    }

    private JobKey jobCheck(String jobName,String jobGroup) throws SchedulerException {
        JobKey jobKey = JobKey.jobKey(jobName,jobGroup);
        JobDetail jobDetail = scheduler.getJobDetail(jobKey);
        if (jobDetail == null) {
            throw new ZdException("定时任务不存在");
        }
        return jobKey;
    }



}
